mui.init({
	/*beforeback:function() {
		var parentObj = plus.webview.currentWebview().opener();
		mui.fire(parentObj, 'dataList');
	},*/
});

document.addEventListener("swipeleft", function () {
    console.log('左');
    if(ckNewsData.tabIndex < 4){
        ckNewsData.tabIndex ++;
    }else{
        ckNewsData.tabIndex = 4
    }
	mui('.mui-scroll-wrapper').scroll().scrollTo(0,0,100);
});

document.addEventListener("swiperight", function () {
    console.log('右');
    if(ckNewsData.tabIndex > 1){
        ckNewsData.tabIndex --;
    }else{
        ckNewsData.tabIndex = 1
    }
    mui('.mui-scroll-wrapper').scroll().scrollTo(0,0,100);
});


document.addEventListener("swipeup", function () {
    if(mui('#scroll').scroll().y <= mui('#scroll').scroll().maxScrollY){
        ckNewsData.getCkdetails('up');
    }
});


document.addEventListener("touchstart", function (event) {
    console.log('开始触摸屏幕');
});


document.addEventListener("touchmove", function (event) {
    if(ckNewsData.openDown == 1){
        if(mui('#scroll').scroll().y >= 0){
            ckNewsData.downData = 1;
            ckNewsData.downText = '下拉以刷新数据...'
        }
        if(mui('#scroll').scroll().y >= 15){
            ckNewsData.downData = 1;
            ckNewsData.downText = '松开手指以刷新...'
        }
    }
});

document.addEventListener("touchend", function (event) {
    if(mui('#scroll').scroll().y >= 20){
        ckNewsData.downData = 0;
        ckNewsData.getCkdetails('down');
    }
    if(mui('#scroll').scroll().y > 0){
        ckNewsData.openDown = 1;
    }else{
        ckNewsData.openDown = 0;
    }
});

var page = 1;
var ckNewsData;
mui.plusReady(function () {
    ckNewsData = new Vue({
        el: '#list_box',
        data: {
            anli_list: [],
            celun_list: [],
            gongwen_list: [],
            keguan_list: [],
            upData: 0,
            downData: 0,
            downText: '',
            openDown:1,  //该状态是否可以激活下拉加载

            tabIndex: "1"
        },
        mounted() {
            this.getCkdetails('normal');
            this.$nextTick(function () {
                mui('.mui-scroll-wrapper').scroll({
                	indicators: false,
                    deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
                });
            })
        },
        methods: {
            getCkdetails: function (type) {
                if (type == 'up') {
                    page++;
                    this.upData = 1;
                } else if (type == 'down') {
                    page = 1;
                    this.anli_list.length = 0;
                    this.celun_list.length = 0;
                    this.gongwen_list.length = 0;
                    this.keguan_list.length = 0;
                }else if(type == 'normal'){
                    showWatting();
                }
                var api_host = plus.storage.getItem('HOST_URL');
                mui.ajax({
                    url: api_host + '/api/linxuan/news/allexamtype',
                    data: {
                        'currentpage': page,
                    },
                    dataType: 'json',
                    type: 'get',
                    timeout: 3000,
                    success: function (msg) {
                        if (msg.code == 1) {
                            switch (type) {
                                case 'up':
                                    $.each(msg.data.anli.text, function (index, value) {
                                        ckNewsData.anli_list.text.push(value);
                                    })
                                    $.each(msg.data.celun.text, function (index, value) {
                                        ckNewsData.celun_list.text.push(value);
                                    })
                                    $.each(msg.data.gongwen.text, function (index, value) {
                                        ckNewsData.gongwen_list.text.push(value);
                                    })
                                    $.each(msg.data.keguan.text, function (index, value) {
                                        ckNewsData.keguan_list.text.push(value);
                                    })
                                    break;
                                case 'down':
                                    ckNewsData.anli_list = msg.data.anli;
                                    ckNewsData.celun_list = msg.data.celun;
                                    ckNewsData.gongwen_list = msg.data.gongwen;
                                    ckNewsData.keguan_list = msg.data.keguan;
                                    mui.toast('刷新成功!', {duration: '200', type: 'div'});
                                    break;
                                case 'normal':
                                    ckNewsData.anli_list = msg.data.anli;
                                    ckNewsData.celun_list = msg.data.celun;
                                    ckNewsData.gongwen_list = msg.data.gongwen;
                                    ckNewsData.keguan_list = msg.data.keguan;
                                    break;
                            }
                        } else {
                            mui.toast(msg.msg, {duration: '200', type: 'div'});
                        }
                    },
                    error: function (xhr, type, errorThrown) {
                        mui.toast('加载失败!', {duration: '200', type: 'div'});
                    },
                    complete: function (XMLHttpRequest, status) {
                        plus.nativeUI.closeWaiting();
                        if (type == 'up') {
                            ckNewsData.upData = 0;
                        }
                        if (status == 'timeout') {//超时,status还有success,error等值的情况
                            mui.toast('请求超时，请重试', {duration: '300', type: 'div'});
                        }
                    }
                })
            },
            open_details: function (id) {
                mui.openWindow({
                    url: 'detail.html',
                    extras: {
                        'newsid': id
                    },
                    show: {
                        autoShow: true,
                        aniShow: 'pop-in',
                        duration: 400
                    },
                    waiting: {
                        autoShow: false,
                        title: '加载中...',
                        options: {},
                    }
                });
            },
            select_tab: function (e) {
                this.tabIndex = e.target.getAttribute('tab-index');
                mui('.mui-scroll-wrapper').scroll().scrollTo(0,0,100);
            }
        }
    })
})